var addressList;

apiready = function () {

	var header = $api.byId('header');
	$api.fixIos7Bar(header);
	
	//添加监听选择收货地址
	api.addEventListener({
	    name: 'addAddressSuccess'
	}, function(ret){
	    if(ret && ret.value){
	       
	     //  alert(JSON.stringify(ret.value));
	       
	      geDate();
		  	
	    }
	});
	
	
	geDate();
	
  	
}

//获取地址
function geDate(){
    //获取地址    
	getAddress(function(alist){
  		addressList = alist;
  		
  		//显示收货地址列表
  		showaddress();
  	});
};

//显示收货地址
function showaddress(){
     
     var data=[];
     for(var i=0;i<addressList.length;i++){
        data.push({title:addressList[i].mobile,
                  subTitle:addressList[i].address,
                  remark:addressList[i].receiver,
        });    
     
     }

     var obj = api.require('UIListView');
	 obj.open({
	    rect: {
	        x: 0,
	        y: 73,
	        w: api.winWidth,
	        h: 480
	    },
	    data:data,
	    
	    rightBtns: [{
	        bgColor: '#52c09A',
	        title: '修改',
	        titleSize: 12,
	        titleColor: '#fff',
	        iconWidth: 20
	    },{
	        bgColor: '#E76B65',
	        title: '删除',
	        titleSize: 12,
	        titleColor: '#fff',
	        iconWidth: 20
	    }
	    
	    ],
	    
	    styles: {
	        borderColor: '#cccccc',       //列表分割线的颜色
	        item: {
	            bgColor: '#F0F5F5',       //列表项的背景色
	            activeBgColor: '#F5F5F5',     //列表项按下时的背景色
	            height: 55.0,
	            imgWidth: 40,
	            imgHeight: 40,
	            placeholderImg: '',
	            titleSize: 12.0,
	            titleColor: '#000',
	            subTitleSize: 12.0,
	            subTitleColor: '#000', 
	            remarkColor: '#000',
	            remarkSize: 16,
	            remarkIconWidth: 30
	        }
	    },
	    fixedOn: ''
	}, function(ret){
	    var index=ret.index;  //数据索引
	    
	    var ads=addressList[index];   //这条数据
	    
	    //点击内容
	    if(ret.eventType=='clickContent'){
	         
	         //发送选择地址监听事件
	         api.sendEvent({
			    name: 'selectaddress',
			    extra: addressList[index]
			 });
			 api.closeWin();
	    }
	    
	    if(ret.eventType=='clickRightBtn'){
	       var btnindex=ret.btnIndex;   //右边按钮索引
	       if(btnindex==0){
	              //修改
	              openWin('myaddressEdit',{address: ads});
	       }
	       
	       //删除
	       if(btnindex==1){
	            
	               api.confirm({title: '提示',msg:'确定删除改地址吗?',buttons:['确定', '取消']},function(ret,err){
						if(ret.buttonIndex == 1){
							 obj.deleteItem({
								    index: index
								},function(ret){
								    if(ret.status){
								       	alert('删除成功');
								       //删除数据库数据
								       do_ajax('/app/member/deladdress','post',{'addressId':ads._id},function(respose,err){
										if (respose.resultCode == "0") {
								                api.hideProgress();
										} else
											alert(respose.datas);
									});
								    
								    }
								});
							
						}
					});
	            
	       
		          
	       }  //btnindex==1   end
	       
	    }
	    
	    
	});
	
	
	
	obj.setRefreshHeader({
		    loadingImg: 'widget://res/UIListView_arrow.png',
		    bgColor: '#F5F5F5',
		    textColor: '#8E8E8E',
		    textDown: '下拉可以刷新...',
		    textUp: '松开开始刷新...',
		    showTime: true
		},function(ret, err){
		      geDate();
		});
	


};
